//=============================================================================
// Dual MK23 Inventory class
//=============================================================================
class DualMK23W extends DualiesW;

function bool HandlePickupQuery( pickup Item )
{
	if ( Item.InventoryType==Class'MK23W' )
	{
		if( LastHasGunMsgTime < Level.TimeSeconds && PlayerController(Instigator.Controller) != none )
		{
			LastHasGunMsgTime = Level.TimeSeconds + 0.5;
			PlayerController(Instigator.Controller).ReceiveLocalizedMessage(Class'KFMainMessages', 1);
		}

		return True;
	}

	return Super.HandlePickupQuery(Item);
}

function AttachToPawn(Pawn P)
{
	local name BoneName;

	Super.AttachToPawn(P);

	if(altThirdPersonActor == None)
	{
		altThirdPersonActor = Spawn(AttachmentClass,Owner);
		InventoryAttachment(altThirdPersonActor).InitFor(self);
	}
	else altThirdPersonActor.NetUpdateTime = Level.TimeSeconds - 1;
	BoneName = P.GetOffhandBoneFor(self);
	if(BoneName == '')
	{
		altThirdPersonActor.SetLocation(P.Location);
		altThirdPersonActor.SetBase(P);
	}
	else P.AttachToBone(altThirdPersonActor,BoneName);

	if(altThirdPersonActor != None)
		DualMK23WAttachment(altThirdPersonActor).bIsOffHand = true;
	if(altThirdPersonActor != None && ThirdPersonActor != None)
	{
		DualMK23WAttachment(altThirdPersonActor).brother = DualMK23WAttachment(ThirdPersonActor);
		DualMK23WAttachment(ThirdPersonActor).brother = DualMK23WAttachment(altThirdPersonActor);
		altThirdPersonActor.LinkMesh(DualMK23WAttachment(ThirdPersonActor).BrotherMesh);
	}
}

function GiveTo( Pawn Other, optional Pickup Pickup )
{
	local Inventory I;
	local int OldAmmo;
	local bool bNoPickup;

	MagAmmoRemaining = 0;

	for( I = Other.Inventory; I != None; I =I.Inventory )
	{
		if ( MK23W(I) != none )
		{
			if( WeaponPickup(Pickup)!= none )
			{
				WeaponPickup(Pickup).AmmoAmount[0] += Weapon(I).AmmoAmount(0);
			}
			else
			{
				OldAmmo = Weapon(I).AmmoAmount(0);
				bNoPickup = true;
			}

			MagAmmoRemaining = MK23W(I).MagAmmoRemaining;

			I.Destroyed();
			I.Destroy();

			break;
		}
	}

	if ( SRKFWeaponPickup(Pickup) != None && Pickup.bDropped )
	{
		MagAmmoRemaining = Clamp(MagAmmoRemaining + SRKFWeaponPickup(Pickup).MagAmmoRemaining, 0, MagCapacity);
	}
	else
	{
		MagAmmoRemaining = Clamp(MagAmmoRemaining + Class'MK23W'.Default.MagCapacity, 0, MagCapacity);
	}

	Super(Weapon).GiveTo(Other, Pickup);

	if ( bNoPickup )
	{
		AddAmmo(OldAmmo, 0);
		Clamp(Ammo[0].AmmoAmount, 0, MaxAmmo(0));
	}
}

function DropFrom(vector StartLocation)
{
	local int m;
	local Pickup Pickup;
	local Inventory I;
	local int AmmoThrown, OtherAmmo;
	if( !bCanThrow )
		return;
	AmmoThrown = AmmoAmount(0);
	ClientWeaponThrown();
	for (m = 0; m < NUM_FIRE_MODES; m++)
	{
		if (FireMode[m].bIsFiring)
			StopFire(m);
	}
	if ( Instigator != None )
		DetachFromPawn(Instigator);
	if( Instigator.Health > 0 )
	{
		OtherAmmo = AmmoThrown / 2;
		AmmoThrown -= OtherAmmo;
		I = Spawn(Class'MK23W');
		//Log("Drop1"@I);
		I.GiveTo(Instigator);
		//Log("Drop2"@I);
		Weapon(I).Ammo[0].AmmoAmount = OtherAmmo;
		//Log("Drop3"@I);
		MK23W(I).MagAmmoRemaining = MagAmmoRemaining / 2;
		//Log("Drop4"@I);
		MagAmmoRemaining = Max(MagAmmoRemaining / 2, 0);
		//Log("Drop5"@I);
	}

	Pickup = Spawn(Class'MK23WPickup',,, StartLocation);
	if ( Pickup != None )
	{
		Pickup.InitDroppedPickupFor(self);
		Pickup.Velocity = Velocity;
		WeaponPickup(Pickup).AmmoAmount[0] = AmmoThrown;
		if( SRKFWeaponPickup(Pickup)!=None )
			SRKFWeaponPickup(Pickup).MagAmmoRemaining = MagAmmoRemaining;
		if (Instigator.Health > 0)
			WeaponPickup(Pickup).bThrown = true;
	}
	Destroyed();
	Destroy();
}

simulated function bool PutDown()
{
	if ( Instigator.PendingWeapon.class == class'MK23W' )
	{
		bIsReloading = False;
	}

	return Super.PutDown();
}

defaultproperties
{
     MagCapacity=24
     ReloadRate=3.600000
     HudImage=Texture'KFWeaponPackTex.Icon.DualMK_Idle'
     SelectedHudImage=Texture'KFWeaponPackTex.Icon.DualMK_inuse'
     Weight=4.000000
     bTorchEnabled=False
     StandardDisplayFOV=60.000000
     SleeveNum=0
     TraderInfoTexture=Texture'KFWeaponPackTex.Icon.DualMK_Trader'
     ZoomedDisplayFOV=50.000000
     FireModeClass(0)=Class'PerksSimHostOrg.DualMK23WFire'
     FireModeClass(1)=Class'KFMod.NoFire'
     SelectSound=Sound'KF_HandcannonSnd.50AE_Select'
     AIRating=0.450000
     CurrentRating=0.450000
     Description="Dual MK23 handgun. Dual 45's is double the fun."
     DisplayFOV=60.000000
     Priority=6
     GroupOffset=4
     PickupClass=Class'PerksSimHostOrg.DualMK23WPickup'
     PlayerViewOffset=(X=10.000000,Z=-1.500000)
     BobDamping=3.800000
     AttachmentClass=Class'PerksSimHostOrg.DualMK23WAttachment'
     IconCoords=(X1=250,Y1=110,X2=330,Y2=145)
     ItemName="Old DualMK23"
     Mesh=SkeletalMesh'KFWeaponPackAnims.DualMK23'
     DrawScale=2.500000
     Skins(1)=Combiner'KFWeaponPackTex.MK23.MK23Final'
}
